package com.fay.mymiaosha.controller;

import com.fay.mymiaosha.exception.BizException;
import com.fay.mymiaosha.response.CommonReturnType;
import com.fay.mymiaosha.response.EmBizError;
import com.fay.mymiaosha.service.IOrderService;
import com.fay.mymiaosha.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/18 18:53
 * @Version 1.0
 */
@RestController("order")
@RequestMapping("/order")
public class OrderController extends BaseController{

    @Autowired
    private IOrderService orderService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @PostMapping("/createorder")
    public CommonReturnType create(
            @RequestParam(name = "itemId") Integer itemId,
            @RequestParam(name = "amount") Integer amount,
            @RequestParam(name = "promoId") Integer promoId) throws BizException {
        // login validation
        Boolean is_login = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
        if (is_login == null || !is_login) {
            throw new BizException(EmBizError.USER_NOT_EXIST);
        }
        UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");

        orderService.createOrder(userModel.getId(), itemId, promoId, amount);
        return CommonReturnType.create(null);
    }
}
